/**
 * 
 */
package com.net.shop.car.manager.api.volley.response;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.net.shop.car.manager.api.model.Seller;
import com.net.shop.car.manager.api.volley.Response;
import com.net.shop.car.manager.utils.Constants;
import com.net.shop.car.manager.utils.Constants.ServiceType;
import com.net.shop.car.manager.utils.LogUtils;
import com.net.shop.car.manager.utils.StringUtils;

/**
 * @author Deve
 *
 */
public class SellerListResponse extends Response {
	/**
	 * 
	 */
	private List<Seller> sellers = new ArrayList<Seller>();
	
	/**
	 * 
	 */
	private int totalPages;

	/**
	 * @param type
	 */
	public SellerListResponse() {
		super(Constants.SELLER_LIST);
	}
	
	@Override
	public String[] getContentTags() {
		return new String[]{"sellerlist","totalpages"};
	}
	
	@Override
	public void parseContent(String content, int index) {
		if (index == 0) {
			parseSellersList(content);
		} else {
			this.totalPages = StringUtils.filterIntegerNull(content);
		}
	}
	
	/**
	 * @param content String
	 * 
	 */
	private void parseSellersList(String content) {
		try {
			JSONArray jsonArray = new JSONArray(content);
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				Seller seller = new Seller();
				seller.setLat(StringUtils.filterNull(jsonObject.getString("LAT")));
				seller.setLon(StringUtils.filterNull(jsonObject.getString("LON")));
				seller.setCheckState(StringUtils.filterBoolNull(jsonObject.getString("CHECK_STATE")));
				seller.setGoodNum(StringUtils.filterIntegerNull(jsonObject.getString("GOOD_NUM")));
				seller.setErrNum(StringUtils.filterIntegerNull(jsonObject.getString("ERR_NUM")));
				seller.setMinPrice(StringUtils.filterMoneyNull(jsonObject.getString("MINPRICE")));
				seller.setSellerId(StringUtils.filterNull(jsonObject.getString("ID")));
				seller.setSellNumber(StringUtils.filterNull(jsonObject.getString("SELLER_NUMBER")));
				seller.setDistance(StringUtils.filterIntegerNull(jsonObject.getString("SFZ")));
				seller.setShopAddress(StringUtils.filterNull(jsonObject.getString("SHOP_ADDR")));
				seller.setShopName(StringUtils.filterNull(jsonObject.getString("SHOP_NAME")));
				seller.setShopLogo(StringUtils.filterNull(jsonObject.getString("SHOP_LOGO")));
				seller.setType(ServiceType.getServiceType(jsonObject.getString("SERVICE_TYPE")));
				sellers.add(seller);
			}
		} catch (JSONException e) {
			LogUtils.e(e.getMessage());
		}
	}
	
	public List<Seller> getSellers() {
		return sellers;
	}

	/**
	 * @return the totalPages
	 */
	public int getTotalPages() {
		return totalPages;
	}
}
